package com.hyg.lib_music.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.Music.Music;
import com.hyg.lib_common.Net.CommonHttpUtil;
import com.hyg.lib_music.R;
import com.hyg.lib_music.adapter.MusicCategorySecondAdapter;
import com.hyg.lib_music.adapter.MusicCategotyAdapter;
import com.hyg.lib_music.databinding.ActivityCategorySecondBinding;
import com.hyg.lib_music.modle.CategoryMusicThreeData;
import com.hyg.lib_music.modle.NewMusicCategoryData;
import com.hyg.lib_music.service.MusicService;
import com.jaeger.library.StatusBarUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CategorySecondActivity extends BaseActivity {
    ActivityCategorySecondBinding binding;
    ImageView backIV;
    TextView titleTv, sortsTv, contentTv;
    RecyclerView recyclerView;
    NewMusicCategoryData.DataDTO dataDTO;
    ArrayList<CategoryMusicThreeData.DataDTO> list = new ArrayList<>();
    String name, content, imageUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCategorySecondBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    public void init() {
        initData();
        initView();
    }

    public void initData() {
        StatusBarUtil.setTransparentForImageViewInFragment(CategorySecondActivity.this, null);
        Intent intent = getIntent();
        dataDTO = intent.getParcelableExtra("data");
    }

    public void initView() {
        binding.tvTitle.setText(dataDTO.cateName);
        binding.tvContent.setText(dataDTO.content);
        if (!TextUtils.isEmpty(dataDTO.logoUrl)) {
            Glide.with(this).load(dataDTO.logoUrl).into(binding.ivTopImage);
        }
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.ivContentMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategorySecondActivity.this, CategoryIntroActivity.class);
                intent.putExtra("name", dataDTO.cateName);
                intent.putExtra("content", dataDTO.content);
                intent.putExtra("image", dataDTO.logoUrl);
                startActivity(intent);
            }
        });
        binding.tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategorySecondActivity.this, CategoryIntroActivity.class);
                intent.putExtra("name", dataDTO.cateName);
                intent.putExtra("content", dataDTO.content);
                intent.putExtra("image", dataDTO.logoUrl);
                startActivity(intent);
            }
        });


        getCategoryData(dataDTO.id + "");
//        initRecyclerView();
    }

    private void initRecyclerView() {
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.rcvSorts.setLayoutManager(layoutManager);
        MusicCategorySecondAdapter mAdapter = new MusicCategorySecondAdapter(this, list, dataDTO.logoUrl);
        binding.rcvSorts.setAdapter(mAdapter);
    }


    public void getCategoryData(String id) {
        CommonHttpUtil.getMusicCategoryThreeData(CategorySecondActivity.this, id, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ErrorDialog("连接服务器超时，请稍后重试");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) {
                parseCategoryData(response);
            }
        });
    }

    private void parseCategoryData(Response response) {
        String result = null;
        try {
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("lgb", "parseCategoryData: " + result);
        CategoryMusicThreeData data = JSONObject.parseObject(result, CategoryMusicThreeData.class);
        if (data.code == 200) {
            list = (ArrayList<CategoryMusicThreeData.DataDTO>) data.data;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    initRecyclerView();
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ErrorDialog(data.message);
                }
            });
        }
    }


}